package model

import (
	"ginblog/utils/errmsg"
)

type CommentPraise struct {
		Comment Comment `gorm:"foreignkey:comment_id"`
		User User `gorm:"foreignkey:Uid"`
		BaseModel
		CommentId     int    `gorm:"type:int;not null" json:"comment_id"`
		Uid     int    `gorm:"type:int;not null" json:"uid"`
		Status int `gorm:"type:int; not null" json:"status"`

}

// 新增评论点赞
func CreateCommentPraise(data *CommentPraise)int  {
	err := db.Create(&data).Error
	if err !=nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}

// 查询用户是否点赞了评论 有值200
func IsPraiseComment(uid int , commentId int)(CommentPraise,int) {
	var commentPraise CommentPraise
	db.Select("id,status").Where(&CommentPraise{CommentId: commentId, Uid: uid}).First(&commentPraise)
	if commentPraise.ID > 0  {
		return commentPraise,errmsg.SUCCSE
	}
	return commentPraise,errmsg.ERROR
}


// 取消评论点赞点赞更新
func SaveCommentPraise(data *CommentPraise,uid int) int  {
	var commentPraise CommentPraise
	var maps = make(map[string]interface{})
	maps["status"] = data.Status
	err = db.Model(&commentPraise).Where(&CommentPraise{CommentId: commentPraise.CommentId, Uid: uid}).Update(maps).Error
	if err != nil{
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}




